﻿using System.Drawing.Imaging;
using System.Drawing;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Xml.Linq;

namespace DFGK_WMS.WebAPI.Tools
{
    public static class Utils
    {
        /// <summary>
        /// 生成四位随机数字和字母验证码
        /// </summary>
        /// <returns></returns>
        public static string GenerateRandomCaptcha()
        {
            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            var captcha = new char[4];
            var random = new Random();
            for (int i = 0; i < captcha.Length; i++)
            {
                captcha[i] = chars[random.Next(chars.Length)];
            }
            return new string(captcha);
        }
        /// <summary>
        /// 生成图片
        /// </summary>
        /// <param name="captchaValue">验证码的值</param>
        /// <returns></returns>
        public static byte[] GetCaptchaImage(string captchaValue)
        {
            using var img = new Bitmap(80, 30);
            using var graphics = Graphics.FromImage(img);
            using var font = new Font("Arial", 16);
            using var brush = new SolidBrush(Color.Black);
            graphics.DrawString(captchaValue, font, brush, 10, 5);
            using var stream = new MemoryStream();
            img.Save(stream, ImageFormat.Png);
            return stream.ToArray();
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="customHeaders">标题</param>
        /// <returns></returns>
        public static byte[] ExportToExcel<T>(List<T> data, Dictionary<string, string>? customHeaders = null) where T : class
        {
            // 创建一个新的工作簿（Workbook）
            IWorkbook workbook = new XSSFWorkbook();

            // 在工作簿中创建一个新的工作表（Sheet）
            ISheet sheet = workbook.CreateSheet("Sheet1");

            // 如果 customHeaders 不为空，则使用自定义标题行
            if (customHeaders != null && customHeaders.Count > 0)
            {
                // 创建标题行
                IRow headerRow = sheet.CreateRow(0);
                int columnIndex = 0;
                foreach (var header in customHeaders)
                {
                    // 在标题行中创建单元格，并将自定义标题设置为单元格值
                    headerRow.CreateCell(columnIndex).SetCellValue(header.Value);
                    columnIndex++;
                }

                // 填充数据行
                for (int i = 0; i < data.Count; i++)
                {
                    // 创建新的数据行
                    IRow dataRow = sheet.CreateRow(i + 1);
                    var properties = typeof(T).GetProperties();
                    columnIndex = 0;
                    foreach (var header in customHeaders)
                    {
                        var property = properties.FirstOrDefault(p => p.Name == header.Key);
                        if (property != null)
                        {
                            object? cellValueObj = property.GetValue(data[i]);
                            string cellValue = cellValueObj?.ToString() ?? "";
                            // 在数据行中创建单元格，并将该属性的值设置为单元格值
                            dataRow.CreateCell(columnIndex).SetCellValue(cellValue);
                        }
                        columnIndex++;
                    }
                }
            }
            else
            {
                // 填充数据行，不创建标题行
                for (int i = 0; i < data.Count; i++)
                {
                    IRow dataRow = sheet.CreateRow(i);
                    var properties = typeof(T).GetProperties();
                    int columnIndex = 0;
                    foreach (var property in properties)
                    {
                        object? cellValueObj = property.GetValue(data[i]);
                        string cellValue = cellValueObj?.ToString() ?? "";
                        dataRow.CreateCell(columnIndex).SetCellValue(cellValue);
                        columnIndex++;
                    }
                }
            }

            // 将工作簿写入内存流中，并将其转换为字节数组
            byte[] fileBytes;
            using (MemoryStream stream = new MemoryStream())
            {
                workbook.Write(stream);
                fileBytes = stream.ToArray();
            }

            // 返回Excel文件的字节数组
            return fileBytes;
        }




    }
}
